home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / vgl20.zip / SCROLL.C < prev    next >
C/C++ Source or Header  |  1993-05-18  |  9KB  |  287 lines

  1. /*****************************************************************************
  2.  SCROLL.C
  3.  
  4.  This is a simple demo of how to use VGL to create scrolling tile effects.
  5.  It loads a 320x200 "map" and allows you to pan around it using the arrow
  6.  keys.  The map is stored as a GIF file.  The map is lame.  I tried, but I
  7.  just didn't have the time nor talent to create an "interesting" and detailed
  8.  map.  Sorry...
  9.  
  10.  Once you run this program you can use the arrow keys to scroll around.  It
  11.  makes use of the VGLKEY.C module, so you can try holding down combinations
  12.  of keys to see it in action.  The '+' and '-' keys will increase (double)
  13.  and decrease (half) the scrolling "speed".  The speed is actually how many
  14.  pixels we scroll the tiles per frame.  Pressing 'T' will toggle the display
  15.  of some text that shows the current X, Y, and Speed.  Pressing 'C' will
  16.  toggle the cycling of part of the palette (the colors that the water tile
  17.  happens to use).  Press ESCAPE to exit.
  18.  
  19.  NOTE: Because displaying text is quite time-consuming, the FPS rate will
  20.        likely slow down quite a bit when the text flag is set.  Play with
  21.        it a bit to see how it effects the FPS on your machine.
  22.        For the record, I get 70+ FPS on my 486DX/33 when no text is displayed.
  23.        I get around 45 FPS when the text is turned on.  On my 486DX/50 I
  24.        get 70+ FPS either way.
  25.  
  26.  Finally: this is a demo.  It is not intended to demonstrate the best method
  27.  of doing a tile based game.  It is simply meant to show *one* possible
  28.  method.  I'm sure you'll think of others.  Hopefully this'll get your
  29.  imagination going.
  30.  
  31.  Mark
  32.  morley@camosun.bc.ca
  33. *****************************************************************************/
  34.  
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include "keys.h"
  38. #include "vgl.h"
  39.  
  40. #define MAPW            320             /* Width of the map in tiles        */
  41. #define MAPH            200             /* Height of the map in tiles       */
  42. #define MAXTILES        32              /* Max number of tiles allowed      */
  43.  
  44. char far  VS[64000];                    /* Out virtual screen               */
  45. char far  Tile[MAXTILES][256];          /* Array of tile bitmaps            */
  46. char far  Map[MAPH][MAPW];              /* The map data                     */
  47. char far  Pal[768];                     /* Our palette                      */
  48.  
  49. Intro()
  50. {
  51.   /* Black the palette */
  52.    vglBlack();
  53.  
  54.    /* Load in a font */
  55.    vglLoadFont( "fonts\\tuxedo.fon" );
  56.  
  57.    /* Set text color to bright red */
  58.    vglTextColor( 12 );
  59.  
  60.    /* Position the "cursor" */
  61.    vglGotoXY( 14, 110 );
  62.  
  63.    /* Display a string */
  64.    vglPuts( "VGL Scrolling Demo" );
  65.  
  66.    /* Fade in */
  67.    vglFadeIn( Pal );
  68.  
  69.    /* Delay a bit */
  70.    sleep( 1 );
  71.  
  72.    /* Fade out */
  73.    vglFadeOut( Pal );
  74.  
  75.    /* Clear the screen */
  76.    vglClear( 0 );
  77.  
  78.    /* Set text color to bright purple */
  79.    vglTextColor( 13 );
  80.  
  81.    /* Position the "cursor" */
  82.    vglGotoXY( 38, 110 );
  83.  
  84.    /* Display some text */
  85.    vglPuts( "By Mark Morley" );
  86.  
  87.    /* Fade in */
  88.    vglFadeIn( Pal );
  89.  
  90.    /* Delay a bit */
  91.    sleep( 1 );
  92.  
  93.    /* Fade out */
  94.    vglFadeOut( Pal );
  95. }
  96.  
  97. main()
  98. {
  99.    int  cx, cy;                         /* Current X and Y position (tile)  */
  100.    int  ox, oy;                         /* Offset within the current tile   */
  101.    int  x, y;                           /* Used in for loops                */
  102.    int  key;                            /* To get keypresses                */
  103.    int  ok = 1;                         /* Loop until ok==0                 */
  104.    int  s = 2;                          /* Scroll speed (2 pixels per frame)*/
  105.    long t1, t2, n;                      /* For calculating FPS              */
  106.    int  text = 0;                       /* Text display flag                */
  107.    int  c = 0;                          /* Counter for cycling              */
  108.    int  cycle = 0;                      /* Cycle flag                       */
  109.    int  i;                              /* Miscellaneous variable           */
  110.    char buf[20];                        /* For formatting text              */
  111.  
  112.    /* Enter mode 13h */
  113.    vglInit();
  114.  
  115.    /* Load in the map data */
  116.    vglGif( "map.gif", (char far*) Map, 0, 0, 0 );
  117.  
  118.    /* Load a couple tiles.  You can load additonal tiles here.  Color 0 in
  119.       the map represents tile number 0.  Color 1 in the map represents tile
  120.       number 1, and so on.  If you load, say, a brick wall in tile number 2,
  121.       then anywhere color 2 appears in the map you'll see a brick wall. */
  122.    vglGif( "water.gif", Tile[0], Pal, 0, 0 );
  123.    vglGif( "grass.gif", Tile[1], Pal, 0, 0 );
  124.  
  125.    /* Display a little intro screen */
  126.    Intro();
  127.  
  128.    /* Load and set up the font characteristics */
  129.    vglLoadFont( "fonts\\comix.fon" );
  130.    vglShadowColor( 1 );
  131.    vglShadowOn( 1 );
  132.    vglTextColor( 15 );
  133.  
  134.    /* Display the "game" screen directly to video memory */
  135.    vglGif( "scroll.gif", VIDMEM, 0, 0, 0 );
  136.  
  137.    /* We'll start at the top left corner of the map */
  138.    cx = 0;
  139.    cy = 0;
  140.    ox = 0;
  141.    oy = 0;
  142.  
  143.    /* Frame count starts at 0 */
  144.    n = 0;
  145.  
  146.    /* We'll draw into our virtual screen */
  147.    vglBuffer( VS );
  148.  
  149.    /* Display the initial tiles (only so there's something to fade in to) */
  150.    for( x = 0; x < 11; x++ )
  151.       for( y = 0; y < 11; y++ )
  152.          vglPut( (x << 4) + 15 - ox, (y << 4) + 15 - oy, 16, 16, Tile[Map[cy + y][cx + x]] );
  153.    vglCopyW( 16, 16, 160, 160 );
  154.  
  155.    /* Fade in from black */
  156.    vglFadeIn( Pal );
  157.  
  158.    /* Enable the trapping of keys */
  159.    vglTrapKeys();
  160.  
  161.    t1 = time( 0 );
  162.  
  163.    /* Loop until we're done */
  164.    while( ok )
  165.    {
  166.       /* Increment the frame count */
  167.       n++;
  168.  
  169.       /* Update the real screen */
  170.       vglCopyW( 16, 16, 160, 160 );
  171.  
  172.       /* Draw the tiles */
  173.       for( x = 0; x < 11; x++ )
  174.          for( y = 0; y < 11; y++ )
  175.             vglPut( (x << 4) + 15 - ox, (y << 4) + 15 - oy, 16, 16, Tile[Map[cy + y][cx + x]] );
  176.  
  177.       /* If the text flag is set, display some coordinate info */
  178.       if( text )
  179.       {
  180.          sprintf( buf, "X=%d Y=%d S=%d", (cx << 4) + ox, (cy << 4) + oy, s );
  181.          vglGotoXY( 18, 25 );
  182.          vglPuts( buf );
  183.       }
  184.  
  185.       /* If a key is hit, check it out */
  186.       if( kbhit() )
  187.       {
  188.          if( (key = getch()) == 0 )
  189.             key = 256 * getch();
  190.          switch( key )
  191.          {
  192.             case Escape            : ok = 0;
  193.                                      break;
  194.             case 't'               :
  195.             case 'T'               : text = 1 - text;
  196.                                      break;
  197.             case 'c'               :
  198.             case 'C'               : cycle = 1 - cycle;
  199.                                      break;
  200.             case '+'               : if( s < 16 )
  201.                                         s <<= 1;
  202.                                      break;
  203.             case '-'               : if( s > 1 )
  204.                                         s >>= 1;
  205.                                      break;
  206.          }
  207.       }
  208.  
  209.       /* Check the arrow key status bytes and change the cx,cy and ox,oy
  210.          variables as appropriate */
  211.       if( vglKeyStatus[0] )
  212.       {
  213.          for( i = 0; i < s; i++ )
  214.          {
  215.             if( oy )
  216.                oy--;
  217.             else if( cy  )
  218.             {
  219.                oy = 15;
  220.                cy--;
  221.             }
  222.          }
  223.       }
  224.       if( vglKeyStatus[1] )
  225.       {
  226.          for( i = 0; i < s; i++ )
  227.          {
  228.             if( oy < 15 )
  229.                oy++;
  230.             else if( cy < MAPH - 11 )
  231.             {
  232.                oy = 0;
  233.                cy++;
  234.             }
  235.          }
  236.       }
  237.       if( vglKeyStatus[2] )
  238.       {
  239.          for( i = 0; i < s; i++ )
  240.          {
  241.             if( ox )
  242.                ox--;
  243.             else if( cx )
  244.             {
  245.                ox = 15;
  246.                cx--;
  247.             }
  248.          }
  249.       }
  250.       if( vglKeyStatus[3] )
  251.       {
  252.          for( i = 0; i < s; i++ )
  253.          {
  254.             if( ox < 15 )
  255.                ox++;
  256.             else if( cx < MAPW - 11 )
  257.             {
  258.                ox = 0;
  259.                cx++;
  260.             }
  261.          }
  262.       }
  263.  
  264.       /* If the cycle flag is set, cycle the palette */
  265.       if( cycle && ++c == 8 )
  266.       {
  267.          c = 0;
  268.          vglPartCycleR( 232, 8, &Pal[232 * 3] );
  269.       }
  270.    }
  271.    t2 = time( 0 );
  272.  
  273.    /* Stop trapping the arrow keys */
  274.    vglReleaseKeys();
  275.  
  276.    /* Fade out to black */
  277.    vglFadeOut( Pal );
  278.  
  279.    /* Return to text mode */
  280.    vglTerm();
  281.  
  282.    /* Display some stats */
  283.    printf( "%ld seconds, %ld frames (%ld FPS)\n", t2 - t1, n, n / (t2 - t1 ) );
  284.  
  285.    return;
  286. }
  287.